package com.mxpio.erp.asset.service.impl;

import com.mxpioframework.jpa.JpaUtil;
import org.springframework.stereotype.Service;

import com.mxpio.erp.common.asset.entity.BorrowOutMaterialFlow;
import com.mxpio.erp.common.asset.service.BorrowOutMaterialFlowService;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class BorrowOutMaterialFlowServiceImpl extends BaseServiceImpl<BorrowOutMaterialFlow> implements BorrowOutMaterialFlowService{
    @Override
    @Transactional(readOnly = true)
    public BigDecimal getReturnedSumQuantityByLineNo(String applyBizNo, String applyBizLineNo) {
        String sumSql = "SELECT ifnull(SUM(QUANTITY_),0) AS QUANTITY_ FROM mb_erp_asset_br_material_flow WHERE access_type_ = '1' and origin_biz_no_ = '" + applyBizNo + "' AND origin_biz_line_no_ = '" + applyBizLineNo + "'";
        BigDecimal sum = (BigDecimal) JpaUtil.nativeQuery(sumSql).getSingleResult();
        return (sum==null?BigDecimal.ZERO:sum);
    }

    @Override
    @Transactional(readOnly = true)//查询借出流水对应的已归还数量合计
    public BigDecimal getSingleOutFlowLotReturnedSum(String applyBizNo, String applyBizLineNo,String lotNo) {
        String sumSql = "SELECT ifnull(SUM(QUANTITY_),0) AS QUANTITY_ FROM mb_erp_asset_br_material_flow WHERE access_type_ = '1' and origin_biz_no_ = '" + applyBizNo + "' AND origin_biz_line_no_ = '" + applyBizLineNo + "'  AND lot_no_ = '" + lotNo + "'";
        BigDecimal sum = (BigDecimal) JpaUtil.nativeQuery(sumSql).getSingleResult();
        return (sum==null?BigDecimal.ZERO:sum);
    }
}
